﻿using BZ.Logger;
using NDK.Module.Model;
using NDK.Motion.BLL;
using NDK.Motion.MachineManager;
using NDK.Motion.Monitor;
using System;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using ThridLibray;

namespace NDK.Module.StationLogic
{
    public class Station_产品扫码 : StationLogic
    {
        #region 通用
        public override event PropertyChangedEventHandler PropertyChanged;

        public static Station_产品扫码 Instance = new Station_产品扫码();
        public Stopwatch SunWay12 = new Stopwatch();
        public string StationName = "Station12产品扫码";
        public bool[] ArrayHoleIsHave;

        public bool IsHaveProduct;


        public int PutCount;

        //步骤字段、属性
        private StationStep _Step = StationStep.判断工位是否屏蔽;
        [Monitor(Description = "当前步骤")]
        public StationStep Step
        {
            get
            {
                return _Step;
            }
            set
            {
                if (_Step != value)
                {
                    if (!Common.BP_是否屏蔽计时)
                    {
                        WriteStationCTLog("StationCTLog", $"{_Step}用时为：{swStepTime.ElapsedMilliseconds} ms");
                        swStepTime.Restart();
                    }
                    _Step = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(value.ToString()));
                }
            }
        }
        #endregion

        /// <summary>
        /// 工站初始化
        /// </summary>
        public override void Init()
        {
            base.Init();
            IsHaveProduct = false;
            SignalComplete = false;
            SignalRequest = false;
            PutCount = 0;
            ArrayHoleIsHave = new bool[2];
            logBaseInfo = "产品扫码流程";
            QueueSN = new ConcurrentQueue<string>();
            Step = StationStep.判断工位是否屏蔽;

        }

        public override void ActionProcess()
        {
            try
            {
                switch (Step)
                {
                    case StationStep.判断工位是否屏蔽:
                        if (Common.BP_屏蔽下料穴1 && Common.BP_屏蔽下料穴2)
                        {
                            AlarmPause("屏蔽选择错误", "当前参数同时屏蔽了两个下料治具，请确认", "重新确认屏蔽载具", "重试");
                        }
                        else
                        {
                            if (PutCount == 0 && Common.BP_屏蔽下料穴1)
                            {
                                PutCount = 1;
                                Step = StationStep.轴运动至接料位;
                                break;
                            }
                            if (PutCount == 1 && Common.BP_屏蔽下料穴2)
                            {
                                PutCount = 0;
                                Step = StationStep.等待下工位要料;
                                break;
                            }
                            Step = StationStep.轴运动至接料位;
                        }
                        break;

                    case StationStep.轴运动至接料位:
                        if (MotionControl.AxesMove(Machine.PosArray_扫码工位接料[PutCount]))
                        {
                            Step = StationStep.等待上工位放料;
                        }
                        break;

                    case StationStep.等待上工位放料:
                        if (Station_转盘下料.Instance.SignalComplete)
                        {
                            SignalRequest = true;
                            if (!Common.IsEmptyModel)
                            {
                                ArrayHoleIsHave[PutCount] = true;
                            }
                            Step = StationStep.等待上工位放料完成;
                        }
                        break;

                    case StationStep.等待上工位放料完成:
                        if (!Station_转盘下料.Instance.SignalComplete)
                        {
                            SignalRequest = false;
                            Step = !IsHaveProduct ? StationStep.等待上工位放料 : StationStep.轴运动至扫码位;
                        }
                        break;

                    case StationStep.轴运动至扫码位:
                        if (MotionControl.AxesMove(Machine.PosArray_扫码工位扫码[PutCount]))
                        {
                            Step = Common.IsEmptyModel || Common.BP_屏蔽扫码 ? StationStep.判断当前放料次数 : StationStep.触发扫码枪扫码;
                        }
                        break;

                    case StationStep.触发扫码枪扫码:
                        var sn = Common.ScanReader.TrigReader(2000).Trim();
                        if(sn != "")
                        {
                            if (Common.BP_UPMES)
                            {
                                var upLoadResult = Common.SwMes.DataInspection(sn);
                                if (!upLoadResult.Item1)
                                {
                                    if(AlarmPause("扫码过站报警", upLoadResult.Item2, upLoadResult.Item3, "重新检测", "忽略报警，继续运行") == DialogResult.OK)
                                    {
                                        break;
                                    }
                                    else
                                    {
                                        sn = "CheckNG";
                                    }
                                }
                                else
                                {
                                    UILog_Alert($"{sn}过站检OK");
                                }
                            }
                        }
                        else
                        {
                            if(AlarmPause("扫码报警", $"扫码枪异常，请检查", "调整扫码位置", "重新检测", "忽略报警，继续运行") == DialogResult.OK)
                            {
                                break;
                            }
                            else
                            {
                                sn = $"Test{DateTime.Now:yyyyMMdd_HHmmss}_{PutCount + 1}";
                            }
                        }
                        QueueSN.Enqueue(sn);
                        Step = StationStep.判断当前放料次数;
                        break;

                    case StationStep.判断当前放料次数:
                        if (PutCount < 1)
                        {
                            if (Common.IsClearModel && Common.TurnTableNone && !Station_下置激光.Instance.IsHaveProduct)
                            {
                                Step = StationStep.等待下工位要料;
                                break;
                            }
                            PutCount++;
                            Step = StationStep.判断工位是否屏蔽;
                        }
                        else
                        {
                            PutCount = 0;
                            Step = StationStep.等待下工位要料;
                        }
                        break;

                    case StationStep.等待下工位要料:
                        if (Station_下料搬运.Instance.SignalRequest)
                        {
                            Station_下料搬运.Instance.QueueHoleIsHave.Enqueue(new bool[2] { ArrayHoleIsHave[0], ArrayHoleIsHave[1] });
                            Step = StationStep.轴运动至下料位;
                        }
                        break;

                    case StationStep.轴运动至下料位:
                        if (MotionControl.AxesMove(Machine.pos_取料位_扫码工位))
                        {
                            SignalComplete = true;
                            Step = StationStep.等待下工位取料完成;
                        }
                        break;

                    case StationStep.等待下工位取料完成:
                        if (!Station_下料搬运.Instance.SignalRequest)
                        {
                            SignalComplete = false;
                            ArrayHoleIsHave = new bool[2];
                            Step = StationStep.判断工位是否屏蔽;
                        }
                        break;

                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                UILog_Error($"[{logBaseInfo}] {ex.Message}");
            }
        }

        public override void EmptyActionProcess()
        {
            ActionProcess();
        }

        public override void GRRActionProcess()
        {
            ActionProcess();
        }

        public enum StationStep
        {
            判断工位是否屏蔽,
            轴运动至接料位,
            等待上工位放料,
            等待上工位放料完成,
            轴运动至扫码位,
            触发扫码枪扫码,
            判断当前放料次数,
            等待下工位要料,
            轴运动至下料位,
            等待下工位取料完成
        }
    }

}
